package com.yami.shop.rights.common.strategy.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yami.shop.bean.enums.RightsType;
import com.yami.shop.bean.event.AddRightsEvent;
import com.yami.shop.bean.event.TransferRightsEvent;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.param.SendUserDropsParam;
import com.yami.shop.bean.param.UserUpdateParam;
import com.yami.shop.rights.common.dto.PersonRightsDto;
import com.yami.shop.rights.common.model.*;
import com.yami.shop.rights.common.service.PersonRightsKtService;
import com.yami.shop.rights.common.service.PersonRightsService;
import com.yami.shop.rights.common.service.ProdRightsKtService;
import com.yami.shop.rights.common.strategy.PersonRightsStrategy;
import com.yami.shop.security.common.util.AuthUserContext;
import com.yami.shop.service.BusinessNftService;
import com.yami.shop.service.ShopDetailService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@Service
public class KtProdRightsStrategyImpl implements PersonRightsStrategy {

    @Autowired
    private ProdRightsKtService prodRightsKtService;
    @Autowired
    private PersonRightsKtService personRightsKtService;
    @Autowired
    private BusinessNftService businessNftService;
    @Autowired
    private ShopDetailService shopDetailService;
    @Autowired
    private PersonRightsService personRightsService;

    @Override
    public String rightsType() {
        return RightsType.KTPROD_VOUCHER.value();
    }

    @Override
    public void savePersonRightsData(ProdRights dto) {
        if (ObjectUtil.isNotEmpty(dto.getPreemptions())) {
            // 修改原有绑定信息
            LambdaUpdateWrapper<ProdRightsKt> prodRightsKtLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            prodRightsKtLambdaUpdateWrapper.eq(ProdRightsKt::getRightsId, dto.getId());
            prodRightsKtLambdaUpdateWrapper.set(ProdRightsKt::getStatus, -1);
            prodRightsKtService.update(prodRightsKtLambdaUpdateWrapper);
            // 新增新的绑定信息
            List<ProdRightsKt> prodRightsKts = new ArrayList<>();
            dto.getPreemptions().forEach(s -> {
                s.setRightsId(dto.getId());
                s.setStatus(1);
                ProdRightsKt prodRightsKt = BeanUtil.copyProperties(s, ProdRightsKt.class);
                // 默认空投数为 1
                prodRightsKt.setNum(1L);
                prodRightsKts.add(prodRightsKt);
            });
            prodRightsKtService.saveBatch(prodRightsKts);
        }

    }

    @Override
    public Long getSurplusTimes(long id) {
        QueryWrapper<PersonRightsKt> personRightsKtQueryWrapper = new QueryWrapper<>();
        personRightsKtQueryWrapper.eq("person_rights_id",id);
        List<PersonRightsKt> personRightsExternals = personRightsKtService.list(personRightsKtQueryWrapper);
        return ObjectUtil.isNotEmpty(personRightsExternals) ? personRightsExternals.stream().mapToLong(PersonRightsKt::getTimes).sum() : 0L;
    }

    @Override
    public void addPersonRights(ProdRights prodRights, AddRightsEvent event, long personRightsId) {
        log.info("商品空投权益信息" + event.getUserId());
        // 获取空投商品
        QueryWrapper<ProdRightsKt> prodRightsKtQueryWrapper = new QueryWrapper<>();
        prodRightsKtQueryWrapper.eq("rights_id",prodRights.getId());
        prodRightsKtQueryWrapper.eq("status",1);
        List<ProdRightsKt> list = prodRightsKtService.list(prodRightsKtQueryWrapper);
        if(ObjectUtil.isEmpty(list) || list.isEmpty()){
            return;
        }
        List<String> prodIds = list.stream().map(s->s.getBlindId()+"").collect(Collectors.toList());
        PersonRightsKt personRightsKt = new PersonRightsKt();
        personRightsKt.setPersonRightsId(personRightsId);
        personRightsKt.setTimes(1);
        personRightsKt.setProdId(String.join(",",prodIds));
        personRightsKt.setType(list.get(0).getBlindType());
        personRightsKtService.save(personRightsKt);
        // 空投商品
        UserUpdateParam param = new UserUpdateParam();
        // 商品信息
        List<SendUserDropsParam> userDropsParams = new ArrayList<>();
        list.forEach(s->{
            SendUserDropsParam sendUserDropsParam = new SendUserDropsParam();
            sendUserDropsParam.setTargetId(s.getBlindId());
            sendUserDropsParam.setType("1".equals(s.getBlindType()) ? 0 : 1);
            sendUserDropsParam.setNums((int) s.getNum());
            userDropsParams.add(sendUserDropsParam);
        });
        param.setDrops(userDropsParams);
        // User
        param.setUserIds(Collections.singletonList(event.getUserId()));
        // 权益空投类型
        param.setBusinessType(3);
        // 业务ID
        param.setAirdropBusinessId(personRightsKt.getId());
        param.setMainProdId(event.getProdId());
        ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(prodRights.getCreateAt());
        businessNftService.bindDropById(param, shopDetail);
    }

    @Override
    public List<PersonRightsDto> verificationRights(JSONObject qrCodeData) {
        // 修改次数
        ArrayList<PersonRightsDto> personRightsDtos = new ArrayList<>();
        PersonRightsKt personRightsKtId = personRightsKtService.getById(qrCodeData.getString("personRightsKtId"));
        personRightsKtId.setStatus(1L);
        personRightsKtId.setTimes(0L);
        personRightsKtService.updateById(personRightsKtId);

        PersonRights personRights = personRightsService.getById(personRightsKtId.getPersonRightsId());
        PersonRightsDto personRightsDto = new PersonRightsDto();
        personRightsDto.setRightsId(personRights.getRightsId());
        personRightsDto.setTimes(1);
        personRightsDto.setUserId(personRights.getUserId());
        personRightsDto.setHolderUserId(personRights.getUserId());
        personRightsDto.setRemark("");
        personRightsDto.setStatus("2");
        personRightsDto.setId(personRightsKtId.getPersonRightsId());

        personRightsDto.setStatusUpdate(1);

        personRightsDtos.add(personRightsDto);
        return personRightsDtos;
    }

    @Override
    public void updatePersonRights(Long rights,String personRightsId, String userId, String toUserId) {

    }

    // 空投权益此方法表示空投失败
    @Override
    public void cancelOrder(Long personRightsKtId) {
        PersonRightsKt personRightsKt = personRightsKtService.getById(personRightsKtId);
        personRightsKt.setStatus(4L);
        personRightsKtService.updateById(personRightsKt);
    }

    @Override
    public void updateRightsEffective(List<Long> rightsId) {

    }

    @Override
    public void rightsTransfer(TransferRightsEvent event) {

    }

    public void rightsKtProd(Long prodId,String bindType,String userId,Long shopId,String rightsId){

        // 新增权益信息
        PersonRights personRights = new PersonRights();
        personRights.setRightsId(Long.parseLong(rightsId));
        personRights.setUserId(userId);
        personRights.setStatus(0);
        personRights.setTimes(1);
        // 新增数据
        personRightsService.save(personRights);

        PersonRightsKt personRightsKt = new PersonRightsKt();
        personRightsKt.setPersonRightsId(personRights.getId());
        personRightsKt.setTimes(1);
        personRightsKt.setProdId(prodId.toString());
        personRightsKt.setType(bindType);
        personRightsKtService.save(personRightsKt);
        // 空投商品
        UserUpdateParam param = new UserUpdateParam();
        // 商品信息
        List<SendUserDropsParam> userDropsParams = new ArrayList<>();
        SendUserDropsParam sendUserDropsParam = new SendUserDropsParam();
        sendUserDropsParam.setTargetId(prodId);
        sendUserDropsParam.setType("1".equals(bindType) ? 0 : 1);
        sendUserDropsParam.setNums(1);
        userDropsParams.add(sendUserDropsParam);
        param.setDrops(userDropsParams);
        // User
        param.setUserIds(Collections.singletonList(userId));
        // 权益空投类型
        param.setBusinessType(3);
        // 业务ID
        param.setAirdropBusinessId(personRightsKt.getId());
        ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(shopId);
        businessNftService.bindDropById(param, shopDetail);

    }

}
